package tradutor;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Tradutor {
	BufferedReader br;
	DataInputStream in;
	public Tradutor(String file) throws IOException{
		FileInputStream fstream = new FileInputStream(file);
		in = new DataInputStream(fstream);
		br = new BufferedReader(new InputStreamReader(in));
	}

	public void Traduz() throws IOException{
		String linha;
		System.out.println("INPP");
		while((linha = br.readLine()) != null){
			String[] quads = linha.split("[ 	]");
			traduzQuad(quads);
			System.out.print("\n");

		}
		System.out.println("PARA");
		in.close();
	}

	public void traduzQuad(String[] quads){
		try{
			int tam;
			tam = quads.length;
			String label = " ";
			if(quads[0].length() != 0){
				if(quads[0].charAt(0) == 'L'){
					label = quads[0];
					for(int i = 1; i < quads.length; i++){
						quads[i-1] = quads[i];
					}
					tam = tam - 1;
				}
			}else{
				for(int i = 1; i < quads.length; i++){
					quads[i-1] = quads[i];
				}
				tam = tam - 1;
			}

			for(int i = 0; i < tam; i++){
				//System.out.println(quads[i]);
			}

			if(quads[0].equals("goto")){
				print("_" + quads[1] + " ENTR");
				return;
			}
			//System.out.println(quads[1] + " tam " + tam);
			if(tam == 3 && quads[1].equals( "=") ){
				print("AMEM 1");
				return;
			}

	

			if(quads[0].equals( "iffalse")){
				print("DSVF _" + quads[5]  + "\n");

				if(quads[2].equals(">")){
					print("CMMA 1 " + quads[3]);
					return;
				}

				if(quads[2].equals(">=")){
					print("CMAF 1 " + quads[3]);
					return;
				}

				
				if(quads[2].equals("!=")){
					print("CMDF 0 " + quads[3]);
					return;
				}

				if(quads[2].equals("==")){
					print("CMIF 0 " + quads[3]);
					return;
				}

				if(quads[2].equals("<")){
					print("CMME 0 " + quads[3]);
					return;
				}
				return;
			}

			
			if(quads[0].equals("if")){
				print("DSVF _" + quads[5]  + "\n");
				
				if(quads[2].equals(">")){
					print("CMMA 1 " + quads[3]);
					return;
				}

				if(quads[2].equals(">=")){
					print("CMAF 1 " + quads[3]);
					return;
				}

				if(quads[2].equals("!=")){
					print("CMDF 0 " + quads[3]);
					return;
				}

				if(quads[2].equals("==")){
					print("CMIF 0 " + quads[3]);
					return;
				}

				if(quads[2].equals("<")){
					print("CMME 0 " + quads[3]);
					return;
				}
				
				if(quads[2].equals("<=")){
					print("CMEG 0 " + quads[3]);
					return;
				}
				return;
			}
			
			if(quads[3].equals("+")){
				print( "SOMA" );
				imprimeOperando(quads[0]);
				imprimeOperando(quads[4]);
				return;
			}
			if(quads[3].equals("*")){
				print( "MULT" );
				imprimeOperando(quads[0]);
				imprimeOperando(quads[4]);
				return;
			}

			if(quads[3].equals("-")){
				print( "SUBT" );
				imprimeOperando(quads[0]);
				imprimeOperando(quads[4]);
				return;
			}
			if(quads[3].equals("[")){
				print("CREN 0"); imprimeOperando(quads[4]);
				return;
			}
			if(quads[1].equals("[") && quads[4].equals("=")){
				print("ARMP");
				imprimeOperando(quads[2]);
				print(quads[5]);
				return;
			}
			
			return;

		}
		catch(Exception e){
			e.printStackTrace();
		}

	}

	public void imprimeOperando(String operando){
		if(operando.charAt(0) ==  'i' || operando.charAt(0) == 'j')
			System.out.print(" 1");
		else if (operando.charAt(0) == 't')
			System.out.print(" " + operando.charAt(1));
		else
			System.out.print(" " + operando);
	}

	public void print(String s){
		System.out.print(s);
	}


}
